import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { message } from "antd";
import { useTranslation } from "react-i18next";

import "./index.less";
import { getListSimpleUsers } from "@/api/system/user";
import { atcClassificationApi } from "@/api/erp/drug/atcClassification";
let reload: Function;
let id: string;
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const en_vi = "erp.drugStorage";
	const formSchema: FormSchema[] = [
		{
			label: t(`${en_vi}.atcClassification.atcAdditionalCode`),
			field: "atcAdditionalCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.newSegment`),
			field: "newSegment",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.dddDose`),
			field: "dddDose",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.atcClassification.dddUnit`),
			field: "dddUnit",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.approvalControl`),
			field: "approvalControl",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.frequency`),
			field: "frequency",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.atcClassification.timesPerDay`),
			field: "timesPerDay",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.atcClassification.maxFrequency`),
			field: "maxFrequency",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.atcClassification.dosePerTime`),
			field: "dosePerTime",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.atcClassification.days`),
			field: "days",
			component: "InputNumber"
		},
		{
			label: t(`erp.invtDept.invtDeptMcheck.user`),
			field: "userId",
			component: "ApiSelect",
			componentProps: {
				api: getListSimpleUsers,
				resultField: "data.list",
				labelField: "nickname",
				valueField: "id"
			}
		},
		{
			label: t(`${en_vi}.atcClassification.usageCoefficient`),
			field: "usageCoefficient",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.anesthesia`),
			field: "anesthesia",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.easilyConfused`),
			field: "easilyConfused",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.multiDose`),
			field: "multiDose",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.activeIngredientOrder`),
			field: "activeIngredientOrder",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.atcClassification.drugOrder`),
			field: "drugOrder",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.atcClassification.ingredientRatioOrder`),
			field: "ingredientRatioOrder",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.atcClassification.drugRatioOrder`),
			field: "drugRatioOrder",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.atcClassification.interactionGroup`),
			field: "interactionGroup",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.highAdrRisk`),
			field: "highAdrRisk",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.contraindicationPosition`),
			field: "contraindicationPosition",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.storagePosition`),
			field: "storagePosition",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.atcUsageContentCode`),
			field: "atcUsageContentCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.retailDrug`),
			field: "retailDrug",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassification.suggestedDays`),
			field: "suggestedDays",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.atcClassification.suggestedQuantity`),
			field: "suggestedQuantity",
			component: "InputNumber"
		},
		{
			label: t(`erp.warehouses.status`),
			field: "status",
			component: "RadioGroup",
			componentProps: {
				options: [
					{
						label: t("erp.warehouseGroupType.open"),
						value: "0"
					},
					{
						label: t("erp.warehouseGroupType.close"),
						value: "1"
					}
				]
			}
		}
	];

	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, async data => {
		reload = data.reload;
		await resetFields();
		if (data.record) {
			id = data.record.id;
			setFieldsValue(data.record);
		}
	});

	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 100,
		baseColProps: {
			span: 24
		},
		initialValues: {
			status: "0"
		}
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		let api = atcClassificationApi.atcClassificationCreate;
		if (id) {
			api = atcClassificationApi.atcClassificationUpdate;
		}
		const res = await api({ ...formValue, id });
		if (res) {
			id ? message.success(t("kevin_ui.updateSuccess")) : message.success(t("kevin_ui.addedSuccessfully"));
			reload();
			closeDrawer();
			id = "";
		}
	};
	return (
		<BasicDrawer showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<div className="warehouses">
				<BasicForm register={register}></BasicForm>
			</div>
		</BasicDrawer>
	);
};
export default index;
